package com.besttone.app;

import android.content.ContentValues;
import android.content.SearchRecentSuggestionsProvider;
import android.database.Cursor;
import android.net.Uri;

public class SuggestionProvider extends SearchRecentSuggestionsProvider {
	public static final String AUTHORITY = "com.besttone.app.114SuggestionProvider";
	public static final String[] COLUMNS;
	public static final int MODE = DATABASE_MODE_QUERIES;
	private static String sDatabaseName = "suggestions.db";
	private static int totalRecord;
	private final int MAXCOUNT = 20;

	static {
		String[] arrayOfString = new String[4];
		arrayOfString[0] = "_id";
		arrayOfString[1] = "display1";
		arrayOfString[2] = "query";
		arrayOfString[3] = "date";
		COLUMNS = arrayOfString;
	}

	public SuggestionProvider() {
		//super(contex, AUTHORITY, MODE);
		setupSuggestions(AUTHORITY, MODE);
	}

	private Object[] columnValuesOfWord(int paramInt, String paramString1,
			String paramString2) {
		Object[] arrayOfObject = new Object[4];
		arrayOfObject[0] = Integer.valueOf(paramInt);
		arrayOfObject[1] = paramString1;
		arrayOfObject[2] = paramString2;
		arrayOfObject[3] = paramString1;
		return arrayOfObject;
	}

	public Uri insert(Uri uri, ContentValues values) {
		String selection = "query=?";
		String[] selectionArgs = new String[1];
		selectionArgs[0] = "没有搜索记录";
		super.delete(uri, selection, selectionArgs);
		Cursor localCursor = super.query(uri, COLUMNS, null, null, null);
		if (localCursor!=null && localCursor.getCount() == 0) {
			ContentValues localContentValues = new ContentValues();
			localContentValues.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[0],
							Integer.valueOf(2));
			localContentValues.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[1],
							Long.valueOf(System.currentTimeMillis()));
			localContentValues.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[2],
							"清空搜索记录");
			localContentValues.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[3],
							"清空搜索记录");
			super.insert(uri, localContentValues);
		}
		values.put("date", Long.valueOf(System.currentTimeMillis()));
		Uri localUri = super.insert(uri, values);
		return localUri;
	}

	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Cursor localCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
		Object localObject;
		if (selectionArgs == null || selectionArgs.length == 0)
			if (localCursor.getCount() == 0) {
				ContentValues localContentValues = new ContentValues();
				localContentValues
						.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[0],
								Integer.valueOf(1));
				localContentValues
						.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[1],
								Long.valueOf(System.currentTimeMillis()));
				localContentValues
						.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[2],
								"没有搜索记录");
				localContentValues
						.put(android.provider.SearchRecentSuggestions.QUERIES_PROJECTION_1LINE[3],
								"没有搜索记录");
				super.insert(uri, localContentValues);
			}
		localCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
		totalRecord = localCursor.getCount();
		if (totalRecord > 20)
			truncateHistory(uri, -20 + totalRecord);
		return localCursor;
	}

	protected void truncateHistory(Uri paramUri, int paramInt) {
		if (paramInt < 0)
			throw new IllegalArgumentException();
		String str = null;
		if (paramInt > 0)
			try {
				str = "_id IN (SELECT _id FROM suggestions ORDER BY date ASC LIMIT "
						+ String.valueOf(paramInt) + " OFFSET 1)";
				delete(paramUri, str, null);
				return;
			} catch (RuntimeException localRuntimeException) {
			}
	}
}